﻿Public Class IngresarFactura
    Inherits System.Web.UI.Page
    Dim c As New Conexion
    Dim precio_unitario As String
    Dim acu As Double = 0


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("VariableNombre") = Nothing Then
            Label1.Text = "Tienes que Logearte"
            btnLogout.Visible = False
        Else
            HyperLink1.Visible = False
            Label2.Text = "Bienvenido " + Session("VariableNombre").ToString()

            c.SeleccionarLetras(TextBox6, "select nombres from usuarios where login='" & Session("VariableNombre").ToString() & "'")
            Label2.Text = "Bienvenido " + TextBox6.Text


        End If



        TextBox15.Enabled = True
        TextBox3.Enabled = True
        DropDownList3.Enabled = True
        DropDownList1.Enabled = True
        DropDownList2.Enabled = False
        Image2.Enabled = True
        Button1.Enabled = True
        Button2.Enabled = False
        Button3.Enabled = False

        TextBox12.Enabled = False
        TextBox9.Enabled = False
        TextBox10.Enabled = False
        TextBox11.Enabled = False

    End Sub

    Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogout.Click
        Session("VariableNombre") = Nothing
        Response.Redirect("InicioSesion.aspx")
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim cliente As String = DropDownList3.SelectedItem.Text
        Dim empleado As String = DropDownList1.SelectedItem.Text
        Dim fecha As String = TextBox3.Text
        Dim observacion As String = TextBox15.Text

        c.Seleccionar(TextBox7, "select idempleado from empleados where nombres='" & empleado & "'")

        c.Seleccionar(TextBox13, "select idcliente from clientes where nombres_cliente='" & cliente & "'")

        '    Dim idInventa As Integer = Convert.ToInt32(TextBox8.Text)

        ListBox1.Text = "acu= " & acu
        Label8.Text = "0"
        Dim sentencia As String = "insert into factura values('" & fecha & "'," & TextBox13.Text & ", '0', '0', '0', " & TextBox7.Text & ")"
        c.Insertar_Actualizar_Eliminar(sentencia, "insertados")
        Label1.Text = Conexion.msj_sql_server

        TextBox15.Enabled = False
        TextBox3.Enabled = False
        DropDownList3.Enabled = False
        DropDownList1.Enabled = False
        Image2.Enabled = False
        DropDownList2.Enabled = True

        TextBox12.Enabled = True
        TextBox9.Enabled = True
        TextBox10.Enabled = True
        TextBox11.Enabled = True

        Button1.Enabled = False
        Button2.Enabled = True
        Button3.Enabled = True

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        ' Label8.Text = Label7.Text
        Dim cantidad As String = Replace(TextBox9.Text, ".", ",")
        precio_unitario = Replace(TextBox10.Text, ".", ",")
        Dim cliente As String = DropDownList3.SelectedItem.Text
        Dim total As Double = Convert.ToDouble(cantidad) * Convert.ToDouble(precio_unitario)
        TextBox11.Text = total
        Label7.Text = total
        Label4.Text = total
        acu = Convert.ToDouble(Label8.Text) + total
        Label8.Text = acu

        Dim data As String = Replace(TextBox11.Text, ",", ".")
        TextBox14.Text = data
        c.Seleccionar(TextBox13, "select idcliente from clientes where nombres_cliente='" & cliente & "'")
        c.Seleccionar(TextBox8, "select idfactura from factura where observacionfactura='" & TextBox15.Text & "'")

        Dim producto As String = DropDownList2.SelectedItem.Text
        c.Seleccionar(TextBox12, "select idproducto from productos where descripcion='" & producto & "'")


        ' Dim sentencia As String = "insert into detalle_factura values(4,4,10, 5, 10)"

        Dim sentencia As String = "insert into detalle_factura values(" & TextBox8.Text & "," & TextBox12.Text & ",'" & TextBox9.Text & "', '" & TextBox10.Text & "', '" & TextBox14.Text & "')"
        c.Insertar_Actualizar_Eliminar(sentencia, "insertados")

        Label3.Text = Conexion.msj_sql_server

     

        ListBox1.Items.Add("id_factura =" & TextBox8.Text)
        ListBox1.Items.Add("id_producto =" & TextBox12.Text)
        ListBox1.Items.Add("cantidad =" & TextBox9.Text)
        ListBox1.Items.Add("precio =" & TextBox10.Text)
        ListBox1.Items.Add("total =" & TextBox14.Text)

       
        c.Seleccionar(TextBox16, "select existencias from productos where IdProducto=" & TextBox12.Text)

        ListBox1.Items.Add("existencias =" & TextBox16.Text)

        Dim existencias As Integer = Convert.ToInt64(TextBox16.Text) - Convert.ToInt64(TextBox9.Text)


        sentencia = "update Productos set Existencias=" & existencias & " where IdProducto=" & TextBox12.Text
        c.Insertar_Actualizar_Eliminar(sentencia, "actualizados")



        TextBox15.Enabled = False
        TextBox3.Enabled = False
        DropDownList3.Enabled = False
        DropDownList1.Enabled = False
        Image2.Enabled = False
        DropDownList2.Enabled = True

        TextBox12.Enabled = True
        TextBox9.Enabled = True
        TextBox10.Enabled = True
        TextBox11.Enabled = True

        Button1.Enabled = False
        Button2.Enabled = True
        Button3.Enabled = True
    End Sub

    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList2.SelectedIndexChanged
        Dim producto As String = DropDownList2.SelectedItem.Text
        c.Seleccionar(TextBox12, "select idproducto from productos where descripcion='" & producto & "'")
        c.SeleccionarLetras(TextBox10, "select precio from productos where idproducto=" & TextBox12.Text)
        TextBox3.Enabled = False
        DropDownList3.Enabled = False
        DropDownList1.Enabled = False
        Image2.Enabled = False
        DropDownList2.Enabled = True

        TextBox12.Enabled = True
        TextBox9.Enabled = True
        TextBox10.Enabled = True
        TextBox11.Enabled = True

        Button1.Enabled = False
        Button2.Enabled = True
        Button3.Enabled = True
    End Sub


    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        Dim iva As Double = Convert.ToDouble(Label8.Text) * 0.12
        Dim total As Double = Convert.ToDouble(Label8.Text) + iva

        ListBox1.Items.Add("acu = " & acu)
        ListBox1.Items.Add("iva = " & iva)
        ListBox1.Items.Add("total = " & total)
        ListBox1.Items.Add("*7id_factura =" & TextBox8.Text)

        Dim subt As String = Replace(Label8.Text, ",", ".")
        Dim iv As String = Replace(iva, ",", ".")
        Dim tot As String = Replace(total, ",", ".")

        Label4.Text = "" & subt
        Label5.Text = "" & iv
        Label6.Text = "" & tot
        Label7.Text = "" & TextBox8.Text

        Dim sentencia As String = "update factura set subtotal='" & Label4.Text & "', iva='" & Label5.Text & "', total='" & Label6.Text & "' where idfactura=" & TextBox8.Text
        c.Insertar_Actualizar_Eliminar(sentencia, "actualizados")

        Label3.Text = Conexion.msj_sql_server

    End Sub
End Class